package com.wt.wechatTools.room;

import android.content.Context;
import android.os.AsyncTask;
import androidx.lifecycle.LiveData;
import com.wt.wechatTools.room.dao.ContactsDao;
import com.wt.wechatTools.room.dao.UserDao;
import com.wt.wechatTools.room.entity.ContactsEntity;
import com.wt.wechatTools.room.entity.UserEntity;
import java.util.List;

/**
 * 仓库类
 */
public class WorkRepository {
    private static final String TAG = "WorkRepository";
    private LiveData<List<ContactsEntity>> allContactsLive;
    private ContactsDao contactsDao;
    private UserDao userDao;

    public WorkRepository(Context context) {
        WorkDatabase workDatabase = WorkDatabase.getDataBase(context.getApplicationContext());
        contactsDao = workDatabase.getContactsDao();
        userDao = workDatabase.getUserDao();
    }

//----------------------------------------------------UserEntity----------------------------------------------------------------------
    /**
     * 更新用户套餐结束时间
     * @return
     */
    public void updateUserEndTime(String endTime){
         userDao.updateEndTime(endTime);
    }

    /**
     * 获取用户表里的任务批次  TaskBatch
     * @return
     */
    public Integer getUserTaskBatch(){
        return userDao.getTaskBatch();
    }

    /**
     * 更新UserEntity
     * @param batch
     */
    public void updateUserTaskBatch(Integer batch)
    {
        userDao.updateTaskBatch(batch);
    }

    /**
     * 更新UserEntity
     * @return
     */
    public void updateUser(UserEntity userEntity){
         userDao.update(userEntity);
    }


    /**
     * 获取用户信息 普通
     * @return
     */
    public  UserEntity getUser()
    {
        return userDao.getUser();
    }

    /**
     * 获取用户信息 Livedata
     * @return
     */
    public  LiveData<UserEntity> getUserLive()
    {
        return userDao.getUserLive();
    }

    /**
     * 删除 用户信息
     */
    public void deleteUserAll()
    {
        userDao.deleteUserAll();
    }

    /**
     * 初始化用户信息
     * @param userEntity
     */
    public void insertUsers(UserEntity userEntity)
    {
        userDao.insert(userEntity);
    }


//----------------------------------------------------ContactsEntity----------------------------------------------------------------------



    /**
     * 清除所有数据
     */
    public void deleteContactsEntityAll()
    {
        contactsDao.deleteAll();
    }


    /**
     * 根据任务批次查询数据
     * @return
     */
    public List<ContactsEntity>  getContactsByTastBatch(Integer batch){
        return contactsDao.getContactsByTastBatch(batch);
    }

    /**
     * 根据任务批次查询数据
     * @return
     */
    public LiveData<List<ContactsEntity>>  getContactsByTastBatchLive(Integer batch){
        return contactsDao.getContactsByTastBatchLive(batch);
    }

    /**
     * 获取任务清单，如果任务清单数量不足，那么从未使用中获取数据补充
     * @param count
     * @return
     */
    public List<ContactsEntity> getTaskList(Integer count){
        List<ContactsEntity> cl =   contactsDao.getState5(count);
        if(cl.size()<count)
        {
            List<ContactsEntity> c2 =  contactsDao.getByCount(count-cl.size());
            for (ContactsEntity c3:c2)
            {
                cl.add(c3);
            }
        }
        return cl;
    }

    /**
     * 同步的 获取通讯录第一行数据
     * @return
     */
    public ContactsEntity getFirst()
    {
        return contactsDao.getFirst();
    }

    /**
     * 插入 ContactsEntity
     * @param contactsEntities
     */
    public void insert(ContactsEntity... contactsEntities) {
        contactsDao.insert(contactsEntities);
    }

    /**
     * 根据手机号查询
     * @param cellPhone
     * @return
     */
    public ContactsEntity getContactsByCellPhone(String cellPhone)
    {
        return  contactsDao.getContactsByCellPhone(cellPhone);
    }

    //获取本地所有数据
    public LiveData<List<ContactsEntity>> getAllContactsLive() {
        return contactsDao.getAllLive();
    }

    /**
     * 获取所有的名单数据
     * @return
     */
    public List<ContactsEntity> getAllContacts() {
        return contactsDao.getAll();
    }

    /**
     * 获取不包含未使用状态的名单数据
     * @return
     */
    public List<ContactsEntity> getContacts() {
        return contactsDao.get();
    }

    //插入
    public void insertContacts(ContactsEntity... contactsEntities) {
        new InsertAsyncTack(contactsDao).execute(contactsEntities);
    }

    //插入
    public void insertList(List<ContactsEntity> contactsEntities) {
        contactsDao.insertList(contactsEntities);
    }

    /**
     * 更新
     * @param contactsEntities
     */
    public void updateContact(ContactsEntity... contactsEntities) {
        new UpdatetAsyncTack(contactsDao).execute(contactsEntities);
    }

    /**
     * 删除  线程的
     * @param contactsEntities
     */
    public void deleteContact(ContactsEntity... contactsEntities) {
        new DeleteAsyncTack(contactsDao).execute(contactsEntities);
    }

    /**
     * 删除
     * @param contactsEntities
     */
    public void deleteContactNormal(ContactsEntity... contactsEntities) {
       contactsDao.delete(contactsEntities);
    }

    /**
     * 根据状态查询手机号
     * @param state
     * @return
     */
    public LiveData<List<ContactsEntity>> getContactsByStateLife(Integer state)
    {
        return contactsDao.getContactsByStateLife(state);
    }

    /**
     * 根据状态查询手机号
     * @param state
     * @return
     */
    public List<ContactsEntity> getContactsByState(Integer state)
    {
        return contactsDao.getContactsByState(state);
    }

    /**
     * 查询 状态 2和3（已执行和无法查找）
     * @return
     */
    public LiveData<List<ContactsEntity>> getContactsBy2and3Life()
    {
        return contactsDao.getContactsBy2and3Life();
    }

    /**
     * 更新 ContactsEntityList
     * @param contactsEntityList
     */
    public void updateContactList(List<ContactsEntity> contactsEntityList) {
        contactsDao.updateList(contactsEntityList);
    }

    //插入
    private static class InsertAsyncTack extends AsyncTask<ContactsEntity, Void, Void> {
        private ContactsDao contactsDao;

        public InsertAsyncTack(ContactsDao contactsDao) {
            this.contactsDao = contactsDao;
        }

        @Override
        protected Void doInBackground(ContactsEntity... contactsEntities) {
            contactsDao.insert(contactsEntities);
            return null;
        }

        //将结果带回主线程
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
        }

        //进度条
        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        //后台任务执行前
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    }

    //更新
    private static class UpdatetAsyncTack extends AsyncTask<ContactsEntity, Void, Void> {
        private ContactsDao contactsDao;

        public UpdatetAsyncTack(ContactsDao contactsDao) {
            this.contactsDao = contactsDao;
        }

        @Override
        protected Void doInBackground(ContactsEntity... contactsEntities) {
            contactsDao.update(contactsEntities);
            return null;
        }

        //将结果带回主线程
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
        }

        //进度条
        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        //后台任务执行前
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

    }

    //删除
    private static class DeleteAsyncTack extends AsyncTask<ContactsEntity, Void, Void> {
        private ContactsDao contactsDao;

        public DeleteAsyncTack(ContactsDao contactsDao) {
            this.contactsDao = contactsDao;
        }

        @Override
        protected Void doInBackground(ContactsEntity... contactsEntities) {
            contactsDao.delete(contactsEntities);
            return null;
        }

        //将结果带回主线程
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
        }

        //进度条
        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        //后台任务执行前
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

    }

//async回调接口    https://blog.csdn.net/hnyzwtf/article/details/51099075
//async基础    https://blog.csdn.net/u013144863/article/details/52390436
}
